
addEvent("cmta:login", true)
addEventHandler("cmta:login", resourceRoot, function(login,pass)
	local result=exports["cmta-db"]:dbGet("SELECT * FROM cmta_users WHERE login=?", login)
    if result and #result > 0 then
        if result[1].login == login and result[1].password == teaEncode(pass,"R{e+=j8'a!EqUT7UKHJS,(E[txHjj35;9aP:l;QjNIpK0$r:Q)") then
            for i,player in pairs(getElementsByType("player")) do
			if getElementData(player,"player:uid") == result[1].id then
                triggerClientEvent(client, "cmta:text", resourceRoot, "#ff0000Ktoś jest już zalogowany na to konto")
                cancelEvent()
			return
			end
            end
            setPlayerName(client, login)
            setElementModel(client,result[1].skin)
            setElementData(client, "player:uid", result[1].id)
            setElementData(client, "player:points", result[1].points)
            setElementData(client, "player:gainscore", result[1].gainscore)
            setElementData(client, "player:timemultiplier", result[1].timemultiplier)
            setElementData(client, "player:timeplayed", result[1].timeplayed)
            setElementData(client, "player:lvl", result[1].level)
            setElementData(client, "player:exp", result[1].exp)
            setElementData(client, "player:po", result[1].PO)
            setElementData(client, "player:dailyclicks", result[1].dailyclicks)
            local resultlvl=exports["cmta-db"]:dbGet("SELECT * FROM cmta_levels WHERE level=?",result[1].level)
            setElementData(client,"player:requestexp",tonumber(resultlvl[1].requestexp))
            setElementData(client,"player:zalogowany",true)
            setElementData(client,"player:logged",true)
            setElementData(client,"player:currentplaytime",0)
            triggerClientEvent(client, "cmta:succeslogin", resourceRoot)
            
            local resultachievement=exports["cmta-db"]:dbGet("SELECT * FROM cmta_achievements WHERE uid=?",result[1].id)
            if resultachievement and #resultachievement < 1 then
            exports["cmta-db"]:dbSet("INSERT INTO cmta_achievements (uid) VALUES (?)", result[1].id)
            exports["cmta-db"]:dbSet("INSERT INTO cmta_inventory (uid) VALUES (?)",result[1].id)
            end
            setElementData(client,"player:achievements",result[1].achievements)
            local lvl = getElementData(client,"player:lvl")
            if lvl >= 50 and lvl < 100 then 
            setPedStat(client, 24, 612)
            setElementHealth(client,110)
            elseif lvl >= 100 and lvl < 150 then 
            setPedStat(client, 24, 652)
            setElementHealth(client,120)
            elseif lvl >= 150 and lvl < 200 then 
            setPedStat(client, 24, 700)
            setElementHealth(client,130)
            elseif lvl >= 200 and lvl < 250 then 
            setPedStat(client, 24, 740)
            setElementHealth(client,140)
            elseif lvl >= 250 and lvl < 300 then 
            setPedStat(client, 24, 783)
            setElementHealth(client,150)
            elseif lvl >= 300 and lvl < 350 then 
            setPedStat(client, 24, 830)
            setElementHealth(client,160)
            elseif lvl >= 350 and lvl < 400 then 
            setPedStat(client, 24, 870)
            setElementHealth(client,170)
            elseif lvl >= 400 and lvl < 450 then 
            setPedStat(client, 24, 913)
            setElementHealth(client,180)
            elseif lvl >= 450 and lvl < 500 then 
            setPedStat(client, 24, 957)
            setElementHealth(client,190)
            elseif lvl >= 500 then
            setPedStat(client, 24, 1000)
            setElementHealth(client,200)
            end

            local premium=exports["cmta-db"]:dbGet("SELECT * FROM cmta_users WHERE id=? AND premium>NOW() LIMIT 1", getElementData(client,"player:uid"))
            if (premium and #premium > 0) then
                setElementData(client,"player:vip",true)
            end

else
       triggerClientEvent(client, "cmta:text", resourceRoot, "#ff0000Podane dane są nieprawidłowe")
        end
    else
        triggerClientEvent(client, "cmta:text", resourceRoot, "#ff0000Takie konto nie istnieje")
    end
    end)


    addEvent("cmta:register", true)
    addEventHandler("cmta:register", resourceRoot, function(login,pass,email,pin)
        local result=exports["cmta-db"]:dbGet("SELECT * FROM cmta_users WHERE login=?", login)
        if result and #result > 0 then
            triggerClientEvent(client, "cmta:regtext", resourceRoot, "#ff0000Login jest już zajęty")
        else
            local query=exports["cmta-db"]:dbSet("INSERT INTO cmta_users (login,password,pin,email,serial) VALUES (?,?,?,?,?)", login, teaEncode(pass,"R{e+=j8'a!EqUT7UKHJS,(E[txHjj35;9aP:l;QjNIpK0$r:Q)"),pin,email,getPlayerSerial(client))
            if query then
            triggerClientEvent(client, "cmta:regtext", resourceRoot, "#00ff00Pomyślnie stworzono konto")
            end
        end
    end)

addEvent("cmta:checkbonus", true)
addEventHandler("cmta:checkbonus", resourceRoot, function()
    exports["cmta-bonusdnia"]:check(client) 
end)